package ui.panels;

import entities.ViewShiftRequest;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import manager.ManagerDriver;
import model.EditShiftRequestTableModel;
import ui.windows.EditShiftRequestDialog;
import ui.windows.RemedySchedulerFrame;

public class HSRManagementPanel extends javax.swing.JPanel {

    /** Creates new form HSRManagementPanel */
    public HSRManagementPanel(RemedySchedulerFrame mainFrame) {
        this.mainFrame = mainFrame;
        driver = (ManagerDriver) mainFrame.getDriver();
        initComponents();
        includeModel();
    }
    
    private void includeModel() {
        model = new EditShiftRequestTableModel(driver.showHeadsRequest());
        requestTable.setModel(model);
        requestTable.setFillsViewportHeight(true);
        requestTable.getTableHeader().setFont(new Font(null, Font.BOLD, 11));
        requestTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
        cellRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer.setBackground(Color.yellow);
        DefaultTableCellRenderer cellRenderer1 = new DefaultTableCellRenderer();
        cellRenderer1.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        cellRenderer1.setBackground(new Color(100, 50, 102));
        cellRenderer1.setForeground(new Color(240, 240, 240));
        requestTable.getColumnModel().getColumn(0).setPreferredWidth(100);
        requestTable.getColumnModel().getColumn(1).setPreferredWidth(150);
        requestTable.getColumnModel().getColumn(2).setPreferredWidth(100);
        requestTable.getColumnModel().getColumn(3).setPreferredWidth(100);
        requestTable.getColumnModel().getColumn(3).setCellRenderer(cellRenderer1);
        requestTable.getColumnModel().getColumn(4).setPreferredWidth(100);
        requestTable.getColumnModel().getColumn(3).setCellRenderer(cellRenderer1);
        requestTable.getColumnModel().getColumn(5).setPreferredWidth(100);
        requestTable.getColumnModel().getColumn(6).setPreferredWidth(100);
        requestTable.getColumnModel().getColumn(6).setCellRenderer(cellRenderer);
    }
    
    private void setSelectedRequest(java.awt.event.MouseEvent evt) {
        int index = requestTable.getSelectedRow();
        if(index != -1) {
            selectedRequest = model.getAt(index);
            if(evt.getClickCount() == 2) {
                EditShiftRequestDialog dialog = new EditShiftRequestDialog(mainFrame, this);
                dialog.setSelectedRequest(selectedRequest);
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
            }
        }
    }
    
    public void setStatusOfSR(int requestID, int status) {
        boolean returnVal = driver.setStatusOfSR(requestID, status);
        if(returnVal) {
            selectedRequest.setStatus(status);
            model.update(selectedRequest);
        } else {
            JOptionPane.showMessageDialog(mainFrame, "Cannot assign !");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private ManagerDriver driver;
    private EditShiftRequestTableModel model;
    private ViewShiftRequest selectedRequest;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbActions = new javax.swing.JComboBox();
        viewControl = new javax.swing.JScrollPane();
        requestTable = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        cbActions.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Actions", "View", "Show Details" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(375, Short.MAX_VALUE)
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        requestTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        requestTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                requestTableMouseClicked(evt);
            }
        });
        viewControl.setViewportView(requestTable);

        add(viewControl, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void requestTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_requestTableMouseClicked
    setSelectedRequest(evt);
}//GEN-LAST:event_requestTableMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbActions;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTable requestTable;
    private javax.swing.JScrollPane viewControl;
    // End of variables declaration//GEN-END:variables
}
